<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/11/16
 * Time: 13:45
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Service\Admin\RoomService;
use App\Constant\ErrorConstant;

class RoomController extends BaseController
{
    protected $service;

    public function __construct(Request $request, RoomService $service)
    {
        parent::__construct($request);

        $this->service  =   $service;
    }

    /**
     * 直播间列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listRoom()
    {
        $page       =   (int) $this->request->input('page', 1);
        $limit      =   (int) $this->request->input('page_size', NULL);
        $status     =   (int) $this->request->input('status', -1);
        $pub_status =   (int) $this->request->input('pub_status', -1);

        [$flag, $result]    =   $this->service->roomList($status, $pub_status, $limit, $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ROOM_LIST_ERROR, $result);
        }
    }

    /**
     * 直播间生成二维码
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\StreamedResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function codeRoom()
    {
        $validator  =   Validator::make($this->request->post(), [
            'box_id'    =>  'integer|exists:mini_box,id',
            'room_id'   =>  'required|integer'  //|exists:mongodb.live,Id
        ], [
            'box_id.required'   =>  '盒子ID不能为空',
            'room_id.required'  =>  '直播间ID不能为空',

            'box_id.integer'    =>  '盒子ID必须为整型',
            'room_id.integer'   =>  '直播间ID必须为整型',

            'box_id.exists'     =>  '盒子不存在',
            'room_id.exists'    =>  '直播间不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ROOM_CODE_ERROR, $validator->errors()->first());
        }

        $room_id    =   (int) $this->request->post('room_id');
        $box_id     =   (int) $this->request->post('box_id');

        [$flag, $result]    =   $this->service->roomCode($room_id, $box_id);
        if ($flag) {
            return response()->stream(function () use ($result) {
                echo $result;
            }, 200, ['Content-Type' => 'image/jpeg']);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ROOM_CODE_ERROR, $result);
        }
    }

    /**
     * 分享直播间
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\StreamedResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function shareRoom()
    {
        $validator  =   Validator::make($this->request->post(), [
            'room_id'   =>  'required|integer'  //|exists:mongodb.live,Id
        ], [
            'room_id.required'  =>  '直播间ID不能为空',
            'room_id.integer'   =>  '直播间ID必须为整型',
            'room_id.exists'    =>  '直播间不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ROOM_CODE_ERROR, $validator->errors()->first());
        }

        $room_id    =   (int) $this->request->post('room_id');

        [$flag, $result]    =   $this->service->roomShare($room_id);
        if ($flag) {
            /*return response()->stream(function () use ($result) {
                echo $result;
            }, 200, ['Content-Type' => 'image/jpeg']);*/
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ROOM_CODE_ERROR, $result);
        }
    }

    /**
     * 直播间详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function detailRoom()
    {
        $validator  =   Validator::make($this->request->post(), [
            'room_id'   =>  'required|integer', //|exists:mongodb.live,_id
        ], [
            'room_id.required'  =>  '直播间ID不能为空',
            'room_id.integer'   =>  '直播间ID必须为整型',
            'room_id.exists'    =>  '直播间不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ROOM_DETAIL_ERROR, $validator->errors()->first());
        }

        $room_id    =   (int) $this->request->post('room_id');
        [$flag, $result]    =   $this->service->roomDetail($room_id);

        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ROOM_DETAIL_ERROR, $result);
        }
    }

    /**
     * 直播间下架
     * @return \Illuminate\Http\JsonResponse
     */
    public function offlineRoom()
    {
        $validator  =   Validator::make($this->request->post(), [
            'room_id'   =>  'required|integer', //|exists:mongodb.live,Id
            'live_type' =>  'required|integer|in:1,2,3'
        ], [
            'room_id.required'  =>  '直播间ID不能为空',
            'room_id.integer'   =>  '直播间ID必须为整型',
            'room_id.exists'    =>  '直播间不存在',

            'live_type.required'=>  '操作类型不能为空',
            'live_type.integer' =>  '操作类型必须为整型',
            'live_type.in'      =>  '操作类型选择错误',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ROOM_OFFLINE_ERROR, $validator->errors()->first());
        }

        $room_id    =   (int) $this->request->post('room_id');
        $live_type  =   (int) $this->request->post('live_type');
        [$flag, $result]    =   $this->service->roomOffline($room_id, $live_type);

        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ROOM_OFFLINE_ERROR, $result);
        }
    }

    /**
     * 创建直播间
     * @return \Illuminate\Http\JsonResponse
     * @throws \AlibabaCloud\Client\Exception\ClientException
     */
    public function createRoom()
    {
        $validator  =   Validator::make($this->request->post(), [
            'live_title'    =>  'required|string',
            'start_time'    =>  'required|date',
            'end_time'      =>  'required|date',
            'live_desc'     =>  'required|string',
            'host_id'       =>  'required|integer|exists:mini_anchor,id',
            'main_img'      =>  'required|string'
        ], [
            'live_title.required'  =>  '直播标题不能为空',
            'start_time.required'  =>  '计划开始时间不能为空',
            'end_time.required'    =>  '计划结束时间不能为空',
            'live_desc.required'   =>  '直播描述不能为空',
            'host_id.required'     =>  '主播不能为空',
            'main_img.required'    =>  '直播主图不能为空',

            'live_title.string'    =>  '直播标题必须为字符',
            'live_desc.string'     =>  '直播描述必须为字符',
            'main_img.string'      =>  '直播主图必须为字符',

            'start_time.date'      =>  '计划开始时间必须为时间格式',
            'end_time.date'        =>  '计划结束时间必须为时间格式',

            'host_id.integer'      =>  '主播ID必须为整型',
            'host_id.exists'       =>  '主播不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_LIVE_CREATE_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->roomCreate($this->request->post());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_LIVE_CREATE_ERROR, $result);
        }
    }

    /**
     * 编辑直播间
     * @return \Illuminate\Http\JsonResponse
     */
    public function updatedRoom()
    {
        $validator  =   Validator::make($this->request->post(), [
            'live_id'       =>  'required|integer',
            'live_title'    =>  'required|string',
            'start_time'    =>  'required|date',
            'end_time'      =>  'required|date',
            'live_desc'     =>  'required|string',
            'host_id'       =>  'required|integer|exists:mini_anchor,id',
            'main_img'      =>  'required|string'
        ], [
            'live_id.required'     =>  '直播间ID不能为空',
            'live_id.integer'      =>  '直播间ID必须为整型',
            'live_title.required'  =>  '直播标题不能为空',
            'start_time.required'  =>  '计划开始时间不能为空',
            'end_time.required'    =>  '计划结束时间不能为空',
            'live_desc.required'   =>  '直播描述不能为空',
            'host_id.required'     =>  '主播不能为空',
            'main_img.required'    =>  '直播主图不能为空',

            'live_title.string'    =>  '直播标题必须为字符',
            'live_desc.string'     =>  '直播描述必须为字符',
            'main_img.string'      =>  '直播主图必须为字符',

            'start_time.date'      =>  '计划开始时间必须为时间格式',
            'end_time.date'        =>  '计划结束时间必须为时间格式',

            'host_id.integer'      =>  '主播ID必须为整型',
            'host_id.exists'       =>  '主播不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_LIVE_CREATE_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->roomUpdate($this->request->post(), (int) $this->request->post('live_id'));
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_LIVE_CREATE_ERROR, $result);
        }
    }

    /**
     * 开启/关闭直播间
     * @return \Illuminate\Http\JsonResponse
     */
    public function startRoom()
    {
        $validator  =   Validator::make($this->request->post(), [
            'room_id'   =>  'required|integer', //|exists:mongodb.live,Id
            'start_type'=>  'required|integer|in:1,2'
        ], [
            'room_id.required'  =>  '直播间ID不能为空',
            'room_id.integer'   =>  '直播间ID必须为整型',
            'room_id.exists'    =>  '直播间不存在',

            'start_type.required'=>  '开始类型不能为空',
            'start_type.integer' =>  '开始类型必须为整型',
            'start_type.in'      =>  '开始类型选择错误',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ROOM_OFFLINE_ERROR, $validator->errors()->first());
        }

        $room_id    =   (int) $this->request->post('room_id');
        $start_type =   (int) $this->request->post('start_type');
        [$flag, $result]    =   $this->service->roomStart($room_id, $start_type);

        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ROOM_OFFLINE_ERROR, $result);
        }
    }
}
